import json

import requests
from django.shortcuts import render, redirect
from django.contrib.auth.backends import ModelBackend
# 对密码加密、解密的工具
from django.contrib.auth.hashers import make_password, check_password
# 自定义用户验证字段
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from django.contrib.auth import get_user_model

User = get_user_model()
from django.db.models.query import QuerySet

import datetime

# 导入rest_framework的文件
import django_filters.rest_framework
from rest_framework import filters
from rest_framework import status
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework import generics
# 导入drf的许可
from rest_framework import permissions
from rest_framework.decorators import api_view, permission_classes
import base64

# jwt生成token相关的信息
from rest_framework_jwt.serializers import jwt_encode_handler, jwt_payload_handler

# 导入model.py中的表格
from apps.resources.models import CarInfoModel, FixedStationInfoModel, TipsInfoModel

# 导入serializers.py中的序列化类
from apps.resources.serializers import CarInfoSerializers, FixedStationInfoSerializers, TipsInfoSerializers

# 导入paginations.py中的分页类
from apps.resources.paginations import CarInfoPagination, FixedStationInfoPagination, TipsInfoPagination


# Create your views here.
class CarInfoViewset(mixins.ListModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
                     mixins.RetrieveModelMixin, mixins.DestroyModelMixin, viewsets.GenericViewSet):
    """
        接口名称：\n
            车辆信息视图集
        请求接口：\n
            api/resources/car
        请求方式及权限：\n
            GET——list——AllowAny
            POST——create——AllowAny
            GET——retrieve——IsAuthenticated
            PUT/PATCH——update——IsAuthenticated
            DELETE——destroy——IsAuthenticated
    """
    queryset = CarInfoModel.objects.all().order_by('-id')
    serializer_class = CarInfoSerializers
    pagination_class = CarInfoPagination
    lookup_field = 'id'

    def get_queryset(self):
        queryset = self.queryset
        search_data = self.request.query_params.get('search_data', None)
        if search_data is not None:
            search_data = json.loads(search_data)
            queryset = CarInfoModel.objects.filter(**search_data).order_by('-id')
        return queryset


class FixedStationInfoViewset(mixins.ListModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
                              mixins.RetrieveModelMixin, mixins.DestroyModelMixin, viewsets.GenericViewSet):
    """
        接口名称：\n
            固定站点信息视图集
        请求接口：\n
            api/resources/fixed-station
        请求方式及权限：\n
            GET——list——AllowAny
            POST——create——AllowAny
            GET——retrieve——IsAuthenticated
            PUT/PATCH——update——IsAuthenticated
            DELETE——destroy——IsAuthenticated
    """
    queryset = FixedStationInfoModel.objects.all().order_by('-id')
    serializer_class = FixedStationInfoSerializers
    pagination_class = FixedStationInfoPagination
    lookup_field = 'id'

    def get_queryset(self):
        queryset = self.queryset
        search_data = self.request.query_params.get('search_data', None)
        if search_data is not None:
            search_data = json.loads(search_data)
            queryset = FixedStationInfoModel.objects.filter(**search_data).order_by('-id')
        return queryset


class TipsInfoViewset(mixins.ListModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
                      mixins.RetrieveModelMixin, mixins.DestroyModelMixin, viewsets.GenericViewSet):
    """
        接口名称：\n
            提醒信息视图集
        请求接口：\n
            api/resources/tips
        请求方式及权限：\n
            GET——list——AllowAny
            POST——create——AllowAny
            GET——retrieve——IsAuthenticated
            PUT/PATCH——update——IsAuthenticated
            DELETE——destroy——IsAuthenticated
    """
    queryset = TipsInfoModel.objects.all().order_by('-id')
    serializer_class = TipsInfoSerializers
    pagination_class = TipsInfoPagination
    lookup_field = 'id'

    def get_queryset(self):
        queryset = self.queryset
        search_data = self.request.query_params.get('search_data', None)
        if search_data is not None:
            search_data = json.loads(search_data)
            queryset = TipsInfoModel.objects.filter(**search_data).order_by('-id')
        return queryset
